package com.zhuzhe.blog.article.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhuzhe.blog.article.requestDto.AdvertDto;
import com.zhuzhe.blog.article.requestDto.AdvertSerachDto;
import com.zhuzhe.blog.article.responseVo.AdvertVo;
import com.zhuzhe.blog.article.service.IAdvertService;
import com.zhuzhe.blog.base.JsonData;
import com.zhuzhe.blog.base.ResponseData;
import com.zhuzhe.blog.convert.Convert;
import com.zhuzhe.blog.article.entity.Advert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 广告信息表 前端控制器
 * </p>
 *
 * @author u53
 * @since 2020-10-08
 */
@Api(value = "广告管理接口", description = "广告管理接口,提供广告的增、删、改、查")
@RestController
@RequestMapping("/advert")
public class AdvertController {
    @Autowired
    private IAdvertService advertService;


    @ApiOperation("根据广告标题与状态查询广告分页列表接口")
    @PostMapping("/search") // /article/advert/search
    public ResponseData<IPage<AdvertVo>> search(@RequestBody AdvertSerachDto advertSerachDto) {
        return advertService.queryPage(advertSerachDto);
    }

    @ApiImplicitParam(name = "id", value = "广告ID", required = true)
    @ApiOperation("删除广告接口")
    @DeleteMapping("/{id}")
    public ResponseData delete(@PathVariable("id") String id) {
        return advertService.deleteById(id);
    }

    @ApiImplicitParam(name = "id", value = "广告ID", required = true)
    @ApiOperation("查询广告详情接口")
    @GetMapping("/{id}")
    public ResponseData<AdvertVo> view(@PathVariable("id") String id) {
        Advert advert = advertService.getById(id);
        if (null == advert){
            return JsonData.fail("无该广告详情");
        }
        AdvertVo advertVo = new AdvertVo();
        Convert.convertType(advert,advertVo);
        return JsonData.success(advertVo);
    }

    @ApiOperation("修改广告信息接口")
    @PutMapping
    public ResponseData update(@RequestBody AdvertDto advertDto) {
        advertDto.setUpdateDate(new Date());
        Advert advert = new Advert();
        Convert.convertType(advertDto,advert);
        advertService.updateById(advert);
        return JsonData.success();
    }

    @ApiOperation("新增广告信息接口")
    @PostMapping
    public ResponseData<Integer> save(@RequestBody AdvertDto advertDto) {
        Advert advert = new Advert();
        Convert.convertType(advertDto,advert);
        advertService.save(advert);
        return JsonData.success(advert.getId());
    }
}
